/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xinxi.dms.view;

import com.xinxi.dms.po.Admin;
import com.xinxi.dms.service.AdminServiceIMPL;
import com.xinxi.dms.service.IAdminService;
import com.xinxi.dms.util.FrameUtil2;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author zhigu
 */
public class AdminFrame extends javax.swing.JInternalFrame {
    IAdminService adminService = new AdminServiceIMPL();
    /**
     * Creates new form AdminFrame
     */
    public AdminFrame() {
        initComponents();
        List<Admin> list = adminService.findAll();
        refresh(list);
        this.btnDelete.setEnabled(false);
        this.btnUpdate.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabAdmin = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtPhone = new javax.swing.JTextField();
        txtFullname = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        btnInto = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("管理员用户系统");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        tabAdmin.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "编号", "用户名", "电话", "真实姓名", "角色"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabAdmin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabAdminMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabAdmin);
        if (tabAdmin.getColumnModel().getColumnCount() > 0) {
            tabAdmin.getColumnModel().getColumn(0).setResizable(false);
            tabAdmin.getColumnModel().getColumn(1).setResizable(false);
            tabAdmin.getColumnModel().getColumn(2).setResizable(false);
            tabAdmin.getColumnModel().getColumn(3).setResizable(false);
            tabAdmin.getColumnModel().getColumn(4).setResizable(false);
        }

        jLabel1.setText("编号：");

        jLabel2.setText("真实姓名：");

        jLabel3.setText("电话：");

        jLabel4.setText("账号：");

        txtId.setEditable(false);

        txtFullname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFullnameActionPerformed(evt);
            }
        });

        btnInto.setText("添加管理员");
        btnInto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIntoActionPerformed(evt);
            }
        });

        btnDelete.setText("删除管理员");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnCancel.setText("取消");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnUpdate.setText("修改管理员");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFullname, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(60, 60, 60)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                                .addComponent(jLabel3))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(btnInto)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(77, 77, 77))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 4, Short.MAX_VALUE)
                                .addComponent(btnUpdate)
                                .addGap(18, 18, 18)
                                .addComponent(btnDelete)
                                .addGap(18, 18, 18)
                                .addComponent(btnCancel)
                                .addContainerGap())))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(96, 96, 96)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(txtFullname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInto)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete)
                    .addComponent(btnCancel))
                .addGap(103, 103, 103))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIntoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIntoActionPerformed
        // TODO add your handling code here:
        //获取输入框中的值
        String name = this.txtName.getText();
        String phone = this.txtPhone.getText();
        String fullname = this.txtFullname.getText();
        Admin admin = new Admin(name,phone,fullname);
        boolean result = adminService.into(admin);
        if(result){
            JOptionPane.showMessageDialog(this, "添加成功");
            cancel();//清空输入框
            
            List<Admin> list = adminService.findAll();
            refresh(list);
        }else
            JOptionPane.showMessageDialog(this, "添加失败");
    }//GEN-LAST:event_btnIntoActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        Integer id = Integer.parseInt(this.txtId.getText());
        boolean result = adminService.delete(id);
        if(result){
            JOptionPane.showMessageDialog(this, "删除成功");
            cancel();//清空输入框
            
            List<Admin> list = adminService.findAll();
            refresh(list);
        }else
            JOptionPane.showMessageDialog(this, "删除失败");
        this.btnInto.setEnabled(true);
        
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        cancel();
        this.btnInto.setEnabled(true);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        String sid = this.txtId.getText();
        Integer id = Integer.parseInt(sid);
        String name = this.txtName.getText();
        String phone = this.txtPhone.getText();
        String fname = this.txtFullname.getText();
        Admin admin = new Admin(id,name,phone,fname);
        boolean result = adminService.update(admin);
        if(result){
            JOptionPane.showMessageDialog(this, "修改成功");
            cancel();//清空输入框
            
            List<Admin> list = adminService.findAll();
            refresh(list);
        }else
            JOptionPane.showMessageDialog(this, "修改失败");
        this.btnInto.setEnabled(true);
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void txtFullnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFullnameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtFullnameActionPerformed

    private void tabAdminMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabAdminMouseClicked
        // TODO add your handling code here:
        int row =this.tabAdmin.getSelectedRow();
        this.txtId.setText(tabAdmin.getValueAt(row, 0).toString());
        this.txtName.setText(tabAdmin.getValueAt(row, 1).toString());
        this.txtPhone.setText(tabAdmin.getValueAt(row, 2).toString());
        this.txtFullname.setText(tabAdmin.getValueAt(row, 3).toString());
        
        this.txtId.setEditable(false);
        this.txtName.setEditable(false);
        
        this.btnInto.setEnabled(false);
        this.btnUpdate.setEnabled(true);
        this.btnDelete.setEnabled(true);
    }//GEN-LAST:event_tabAdminMouseClicked

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
       FrameUtil2.framemap.remove(Admin.class.getName());
    }//GEN-LAST:event_formInternalFrameClosing
    private void cancel(){
        this.txtId.setText("");
        this.txtName.setText("");
        this.txtPhone.setText("");
        this.txtFullname.setText("");
        
        this.txtId.setEditable(true);
        this.txtName.setEditable(true);
        this.btnUpdate.setEnabled(false);
        this.btnDelete.setEnabled(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnInto;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabAdmin;
    private javax.swing.JTextField txtFullname;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPhone;
    // End of variables declaration//GEN-END:variables
private void refresh(List<Admin> list) {
        DefaultTableModel dtm = (DefaultTableModel) this.tabAdmin.getModel();
        while(dtm.getRowCount()>0){
            //从第0行开始删除
            dtm.removeRow(0);
        }//遍历集合
        for(Admin a : list){
            Vector v =new Vector();
            v.add(a.getId());
            v.add(a.getName());
            v.add(a.getPhone());
            v.add(a.getFullname());
            v.add(a.getExtent());
            dtm.addRow(v);
        }
    }
}
